﻿using NDK.Module.DAL;
using NDK.Module.Model;
using NDK.Utils;
using System;
using System.Collections.Generic;
using System.Data;
using System.Drawing;
using System.IO;
using System.Windows.Forms;

namespace NDK.Frm
{
    public partial class Frm_Alarm2 : Form
    {
        private float[] timeArr = new float[24];
        AlarmInfoServices alarmInfoServices = new AlarmInfoServices();

        public Frm_Alarm2()
        {
            InitializeComponent();
        }
        private static Frm_Alarm2 _instance;
        private static readonly object singletonLock = new object();
        public static Frm_Alarm2 Instance
        {
            get
            {
                if (_instance == null)
                {
                    lock (singletonLock)
                    {
                        if (_instance == null)
                        {
                            _instance = new Frm_Alarm2();
                        }
                    }
                }
                return _instance;
            }
        }

        private void Frm_Alarm2_Load(object sender, EventArgs e)
        {
            dtp_StartDate.Text = DateTime.Now.AddDays(-1).ToString();
            dtp_EndDate.Text = DateTime.Now.ToString();
            dtp_StartTime.Text = DateTime.Now.ToString();
            dtp_EndTime.Text = DateTime.Now.ToString();

            List<AlarmInfo> list = alarmInfoServices.GetAllModelList();

            if (list != null)
            {
                foreach (var item in list)
                {
                    if (!cmb_AlarmType.Items.Contains(item.Type))
                    {
                        cmb_AlarmType.Items.Add(item.Type);
                    }
                }
            }

            cmb_AlarmType.SelectedIndex = 0;

            dgv_AlarmInfo.Columns[1].DefaultCellStyle.Format = "yyyy-MM-dd HH:mm:ss";
            dgv_AlarmInfo.Columns[2].DefaultCellStyle.Format = "HH:mm:ss";

            userCurve1.AddLeftAuxiliary(10, Color.Red);
            userCurve1.AddRightAuxiliary(10, Color.Red);

        }

        DataTable dt = null;

        private void btn_Query_Click(object sender, EventArgs e)
        {
            //查询条件
            DateTime startTime = DateTime.Parse(dtp_StartDate.Text + " " + DateTime.Parse(dtp_StartTime.Text).ToString("HH:mm:ss"));
            DateTime endTime = DateTime.Parse(dtp_EndDate.Text + " " + DateTime.Parse(dtp_EndTime.Text).ToString("HH:mm:ss"));


            if (startTime > endTime)
            {
                MessageBox.Show("开始时间与结束时间不符合，请检查", "查询提示");
                return;
            }
            TimeSpan ts = endTime - startTime;
            if (ts.TotalDays > 14.0)
            {
                MessageBox.Show("查询范围太大", "查询提示");
                return;
            }

            dt = alarmInfoServices.GetModelListByStartTimeAndType(startTime, endTime, cmb_AlarmType.Text.Trim());
            dgv_AlarmInfo.DataSource = dt;

            UpdateCurve();

        }

        private void UpdateCurve()
        {
            DateTime startTime2 = DateTime.Parse(dtp_EndDate.Text);

            //从数据库中查询每个小时报警时间  初始化 timeArr
            for (int i = 0; i < 24; i++)
            {
                timeArr[i] = alarmInfoServices.GetAlarmTimeCountByStartTimeAndType(startTime2.AddHours(i), startTime2.AddHours(i + 1), cmb_AlarmType.Text.Trim());
            }

            userCurve1.SetLeftCurve("A", timeArr, Color.DodgerBlue);
            userCurve1.SetCurveText(new string[] { "0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23" });
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string dir = @"D:\Data\AlarmInfo\";
            string fileName = dir+ dtp_StartDate.Text.Replace(" - ", "_") + "-" + dtp_StartTime.Text.Replace(":", "_") + "----" + dtp_EndDate.Text.Replace("-", "_") + "-" + dtp_EndTime.Text.Replace(":", "_") + ".csv";
            if (dt != null && dt.Rows.Count > 0)
            {
                DataTable dt2 = dt.Clone();
                dt2.Columns[0].ColumnName = "序号";
                dt2.Columns[1].ColumnName = "起始时间";
                dt2.Columns[2].ColumnName = "结束时间";
                dt2.Columns[3].ColumnName = "持续时间";
                dt2.Columns[4].ColumnName = "类型";
                dt2.Columns[5].ColumnName = "描述";
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }
                FileUtils.DeleteFile(fileName);
                FileUtils.SaveCSV(dt2, fileName);
                MessageBox.Show("导出完成！");
            }
        }
    }
}
